//
//  GamePlayViewController.m
//  LOLVideoPlayer
//
//  Created by SXW on 16/3/29.
//  Copyright © 2016年 盛宣伟. All rights reserved.
//

#import "GamePlayViewController.h"
#import "MyselfCollectionViewCell.h"
#import "MyselfModel.h"
#import "SXHTTPEngine.h"
#import "DetailViewController.h"
@interface GamePlayViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    
    UICollectionView *mainCollectionView;
}

@end

@implementation GamePlayViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self createCollectionView];
    [self createDataSource];
    
}

- (void)createDataSource{
    
    self.urlStr = [NSString stringWithFormat:@"http://www.demaxiya.com/app/index.php?m=channelmore&id=158&showcount=%ld&__ii=865168026985233&__aa=31ebff81777ab5e3",self.pageNum];
    [[SXHTTPEngine shareManager] GET:self.urlStr params:nil success:^(id responseObject) {
        NSArray *listArr = responseObject[@"list"];
        
        NSArray *arr = [MyselfModel arrayOfModelsFromDictionaries:listArr];
        [self.jsonArray addObjectsFromArray:arr];
        NSLog(@"%ld",self.jsonArray.count);
        
        [mainCollectionView reloadData];
        [self.view addSubview:mainCollectionView];
        
    } failure:^(NSError *error) {
        NSLog(@"请求数据失败");
    }];
}

//加载更多
-(void)loadData{
    
    self.pageNum += 20;
    [self createDataSource];
    [mainCollectionView.mj_footer endRefreshing];
}

//下拉刷新
-(void)refreshData{
    
    self.pageNum = 0;
    [self.jsonArray removeAllObjects];
    [self createDataSource];
    [mainCollectionView.mj_header endRefreshing];
}


- (void)createCollectionView{
    
    self.view.backgroundColor = [UIColor whiteColor];
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    //该方法也可以设置itemSize
    layout.itemSize = CGSizeMake(80, 120);
    //设置边距
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //分区的头视图和尾视图是否始终显示在界面上
    layout.sectionHeadersPinToVisibleBounds = NO;
    layout.sectionFootersPinToVisibleBounds = NO;
    //2.初始化collectionView
    mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -20, APPScreenWidth, APPScreenHeight + 20 - 74 - 49) collectionViewLayout:layout];
    [self.view addSubview:mainCollectionView];
    mainCollectionView.backgroundColor = [UIColor whiteColor];
    
    //3.注册collectionViewCell
    [mainCollectionView registerClass:[MyselfCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    
    //4.设置代理
    mainCollectionView.delegate = self;
    mainCollectionView.dataSource = self;
    
    //下拉刷新
    mainCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    //上拉加载刷新
    mainCollectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
}

#pragma mark - 协议内容

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.jsonArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MyselfCollectionViewCell *cell = (MyselfCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    if (self.jsonArray.count > 0) {
        MyselfModel *model = [self.jsonArray objectAtIndex:indexPath.row];
        [cell configData:model];
    }
    return cell;
}

//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyselfCollectionViewCell *cell = (MyselfCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    NSString *msg = cell.contentLabel.text;
    NSLog(@"%@",msg);
    
    DetailViewController *dvc = [DetailViewController new];
    MyselfModel *model = [self.jsonArray objectAtIndex:indexPath.row];
    dvc.iddd = model.vid;
    
    [self.navigationController pushViewController:dvc animated:YES];
}





















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






@end
